package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-22
 */
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    EduCourseService eduCourseService;

    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String courseId=eduCourseService.addCourseInfo(courseInfoVo);
        return R.ok().data("courseId",courseId);
    }

    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable("courseId") String courseId){
        CourseInfoVo courseInfoVo=eduCourseService.getCourseInfo(courseId);
        return R.ok().data("info",courseInfoVo);
    }

    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }
    //获取发布课程确认信息
    @GetMapping("getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable("courseId") String courseId){
        CoursePublishVo coursePublishVo=eduCourseService.getPublishCourseInfo(courseId);
        return R.ok().data("publish",coursePublishVo);
    }

    //课程发布
    @PostMapping("/publishCourse/{courseId}")
    public R publishCourse(@PathVariable("courseId") String courseId){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(courseId);
        //课程发布实际上就是修改课程发布状态，已发布是Normal，未发布时Draft
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    //课程列表
    @GetMapping("/courseList/{page}/{size}")
    public R courseList(@PathVariable("page") int page,@PathVariable("size") int size){
        Page<EduCourse> iPage=new Page<>(page,size);
        IPage<EduCourse> courseIPage = eduCourseService.page(iPage, null);
        List<EduCourse> courseList = courseIPage.getRecords();
        long total = courseIPage.getTotal();
        return R.ok().data("course",courseList).data("total",total);
    }

    //删除课程
    @PostMapping("deleteCourse/{courseId}")
    public R deleteCourseById(@PathVariable("courseId") String courseId){
        eduCourseService.deleteCourseById(courseId);
        return R.ok();
    }

}

